package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.StudyClassDTO;
import com.fuli.app.dto.query.StudyClassQueryDTO;
import com.fuli.app.service.ClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "班级")
@RestController
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;

    @ApiOperation(value = "添加班级",
            notes = " <b>必传字段：</b><br/>" +
                    "1.name 班级名称<br/> " +
                    "2.amount 班级总人数")
    @PostMapping("/add")
    public ActionResult<String> add(@ApiParam("班级DTO") @RequestBody StudyClassDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getName()))
            errors.add(new ErrorInfo(0, "班级名不能为空！"));
        if (StringUtils.isEmpty(dto.getAmount()))
            errors.add(new ErrorInfo(0, "班级总人数不能为空！"));
        if (!errors.isEmpty())
            return new ActionResult<>(errors);
        return classService.add(dto);
    }
    @ApiOperation("删除班级")
    @DeleteMapping(value = "/delete/{id}")
    public ActionResult<String> delete(@ApiParam("id") @PathVariable String id) {
        return classService.delete(id);
    }

    @ApiOperation("批量删除班级")
    @DeleteMapping(value = "/batchDelete")
    public ActionResult<List<String>> batchDelete(@ApiParam("班级id数组") @RequestParam List<String> ids) {
        return classService.batchDelete(ids);
    }

    @ApiOperation("获取班级")
    @GetMapping(value = "/get/{id}")
    public ActionResult<StudyClassDTO> get(@ApiParam("id") @PathVariable String id) {
        return classService.get(id);
    }

    @ApiOperation("分页获取班级")
    @GetMapping(value = "/pageQuery")
    public ActionResult<Pagination<StudyClassDTO>> listByQueryDTO(@ApiParam("班级查询DTO") @ModelAttribute StudyClassQueryDTO QueryDTO) {
        return classService.listByQueryDTO(QueryDTO);
    }
    
    @ApiOperation(
            value = "修改班级",
            notes = "<b>必填属性：</b><br>" +
                    "1.id 主键<br/>" +
                    "2.name 名称<br/>" +
                    "<br/>"
    )
    @PutMapping(value = "/update")
    public ActionResult<String> update(@ApiParam("班级DTO") @RequestBody StudyClassDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getId()))
            errors.add(new ErrorInfo(0, "id为空"));
        if (StringUtils.isEmpty(dto.getName()))
            errors.add(new ErrorInfo(0, "名称为空"));
        if (!errors.isEmpty())
            return new ActionResult<>(errors);
        return classService.update(dto);
    }
}
